package com.eos.control.action.base;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.eos.common.tool.ConstantTool;
import com.hf.utils.StringUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class AbstractBaseAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private final static int BUFFER_SIZE = 64 * 1024;
	protected String message;
	protected String statusCode;
	protected static final int PAGE_SIZE = 20;

	public String getMessage() {
		return message;
	}

	public String getStatusCode() {
		return statusCode;
	}

	public void outJsonString(String str) {
		getResponse().setContentType("text/javascript;charset=UTF-8");
		outString(str);
	}

	public void outString(String str) {
		try {
			PrintWriter out = getResponse().getWriter();
			out.write(str);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void outXMLString(String xmlStr) {
		getResponse().setContentType("application/xml;charset=UTF-8");
		outString(xmlStr);
	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public HttpSession getSession() {
		return getRequest().getSession();
	}

	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	public ActionContext getContext() {
		return ServletActionContext.getContext();
	}

	public String getRealyPath(String path) {
		return getServletContext().getRealPath(path);
	}

	/**
	 * 针对关键参数判空操作
	 * 
	 * @param parameters
	 * @return 有为空返回true
	 */
	public boolean identification(String... parameters) {
		if (null == parameters) {
			return true;
		}
		for (String parameter : parameters) {
			if (StringUtil.isEmpty(parameter)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 过滤上传文件类型 none表示任意文件类型
	 * 
	 * @param types
	 *            允许上传文件类型集合
	 * @param fileType
	 *            上传文件的类型
	 * @return true 通过验证 false 不允许上传的类型
	 */
	private boolean filterType(String[] types, String fileType) {
		for (String type : types) {
			if ("none".equals(type)) {
				return true;
			}
			if (type.equals(fileType)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 过滤上传文件大小 0表示任意大小文件
	 * 
	 * @param file
	 *            文件
	 * @param allowSizes
	 *            允许上传文件大小
	 * @return true允许上传 false文件过大禁止上传
	 */
	private boolean filterSize(File file, String allowSizes) {
		if (identification(allowSizes) || null == file) {
			return false;
		}
		long filesize = file.length() % 1000 == 0 ? file.length() / 1000 : file
				.length() / 1000 + 1;
		long allowfilesize = StringUtil.parseInt(allowSizes);
		if (allowfilesize == 0)
			return true;
		if (filesize <= allowfilesize)
			return true;
		return false;
	}

	/**
	 * 验证文件的类型和大小
	 * 
	 * @param file
	 *            文件
	 * @param fileType
	 *            上传文件类型
	 * @param allowType
	 *            允许上传类型
	 * @param allowSizes
	 *            允许上传大小
	 * @return
	 */
	public String fileValidation(File file, String fileType, String allowType,
			String allowSizes) {
		if (identification(fileType, allowType, allowSizes) || null == file) {
			return ConstantTool.FILE_FORMAT_ERROR;
		}
		if (!filterType(allowType.split(","), fileType)) {
			return ConstantTool.FILE_FORMAT_ERROR;
		}
		if (!filterSize(file, allowSizes)) {
			return ConstantTool.FILE_SIZE_ERROR;
		}
		return ConstantTool.EXEC_SUCCESS;
	}

	/**
	 * 文件上传写入磁盘的方法
	 * 
	 * @param file
	 *            文件
	 * @param distpath
	 *            写入磁盘的路径
	 * @return
	 * @throws Exception
	 */
	public String fileUpload(File file, String distpath) throws Exception {
		BufferedOutputStream fos = null;
		FileInputStream fis = null;
		FileOutputStream fops = null;
		try {
			fops = new FileOutputStream(distpath);
			fos = new BufferedOutputStream(fops, BUFFER_SIZE);
			fis = new FileInputStream(file);
			byte[] buffer = new byte[BUFFER_SIZE];
			int len = 0;
			while ((len = fis.read(buffer)) > 0)
				fos.write(buffer, 0, len);
		} catch (FileNotFoundException e) {
			return ConstantTool.FILE_NOT_FOUND;
		} catch (IOException e) {
			return ConstantTool.FILE_IO_EXCEPTION;
		} finally {
			if (null != fis)
				fis.close();
			if (null != fos)
				fos.close();
			if (null != fops)
				fops.close();
		}
		return ConstantTool.EXEC_SUCCESS;
	}
}
